<script setup>
import {
    Edit,
    Delete,
    Close
} from '@element-plus/icons-vue'


import { ref } from 'vue'

const resourceTypes = ref([
  {
    "id": 1,
    "resourceValue": "星星",
  },
  {
    "id": 2,
    "resourceValue": "矿石",
  } ,
  {
    "id": 3,
    "resourceValue": "头像",
  },
  {
    "id": 7,
    "resourceValue": "表情包",
  },

  {
    "id": 8,
    "resourceValue": "谢谢惠顾",
  }

])
const types = ref([
    {
        "id": 1,
        "contentValue": "等级",
    },
    {
        "id": 2,
        "contentValue": "里程碑",
    },
    
])

const type = ref('')




const dataList = ref([


])




import {versionSeasonListService ,versionSeaonAddService,versionSeasonService} from '@/api/versionSeason.js'



import { Plus } from '@element-plus/icons-vue'
import { QuillEditor } from '@vueup/vue-quill'
import '@vueup/vue-quill/dist/vue-quill.snow.css'
//控制抽屉是否显示
const visibleDrawer = ref(false)
//添加表单数据模型
const dataModel = ref({

})


//导入token
import { useTokenStore } from '@/stores/token.js';
const tokenStore = useTokenStore();
const queryTime = ref([])


//上传成功的回调函数
const uploadSuccess = (result) => {
  dataModel.value.imgUrl = result.data;
    console.log(result.data);
}

const uploadAudioSuccess = (result) => {
  dataModel.value.audioUrl = result.data;
    console.log(result.data);
}


//添加文章
import {ElMessage, ElMessageBox} from 'element-plus'

import {treeHoleDelService} from "@/api/treeHole.js";
import {interestTopInterestPublishService} from "@/api/interestPublishRecordData.js";
import {
  lotteryAddService,
  lotteryDelService,
  lotteryEditService,
  lotteryListService,
  lotteryPublishService
} from "@/api/lottery.js";
const addData = async (clickState) => {
    //把发布状态赋值给数据模型
    dataModel.value.state = clickState;

    //调用接口
    let result = await lotteryAddService(dataModel.value);

    ElMessage.success(result.msg ? result.msg : '添加成功');

    //让抽屉消失
    visibleDrawer.value = false;

    //刷新当前列表
  lotteryList()
}
const uploadBackgroundUrlSuccess = (result) => {
  dataModel.value.backgroundUrl = result.data.fileUrl;
}

const delBackgroundUrl = () => {
  dataModel.value.backgroundUrl = '';
}

//编辑分类
const updateData = async () => {
    //调用接口
    let result = await lotteryEditService(dataModel.value);

    ElMessage.success(result.msg ? result.msg : '修改成功')

    //调用获取所有分类的函数
  lotteryList();

    //隐藏弹窗
    visibleDrawer.value = false;
}
type.value=1;


//获取文章列表数据
const lotteryList = async () => {
    let params = {

      timeFrom: queryTime.value ? queryTime.value[0] : null,
      timeEnd: queryTime.value ? queryTime.value[1] : null,
    }

    let result = await lotteryListService(params);
   dataList.value = result.data;

}


lotteryList();



//定义变量,控制标题的展示
const title = ref('')
const showData = (row) => {
    
  visibleDrawer.value = true, title.value = '编辑抽奖'
  dataModel.value.type = row.type;
  dataModel.value.num = row.num;
  dataModel.value.name = row.name;
  dataModel.value.backgroundUrl = row.backgroundUrl;
  dataModel.value.sort=row.sort;
  dataModel.value.endTime=row.endTime;
  dataModel.value.id = row.id;


}
const delData = (row) => {
  //提示用户  确认框

  ElMessageBox.confirm(
      '你确认要删除该抽奖吗?',
      '温馨提示',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }
  )
      .then(async () => {
        //调用接口
        let result = await lotteryDelService(row.id);
        ElMessage({
          type: 'success',
          message: '删除成功',
        })
        //刷新列表
        lotteryList();
      })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: '用户取消了删除',
        })
      })
}
//清空模型的数据
const clearData = () => {

  dataModel.value.type = '';
  dataModel.value.num = '';
  dataModel.value.name = '';
  dataModel.value.backgroundUrl = '';
  dataModel.value.sort='';
  dataModel.value.endTime='';
  dataModel.value.id = '';
}

const handleChange = (value) => {
   if(dataModel.value.type ==1){
     dataModel.value.name = "第"+value+"版本";
     dataModel.value.simpleName = 'g'+value;
   }else{
     dataModel.value.name = "第"+value+"赛事";
     dataModel.value.simpleName = 's'+value;
   }
}
const publish = async (row) => {

  let result = await lotteryPublishService(row.id);
  ElMessage.success('置顶成功');
  lotteryList();

}


</script>
<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span>抽奖类目管理</span>
                <div class="extra">
                    <el-button type="primary" @click="visibleDrawer = true; title = '添加抽奖'; clearData()">添加抽奖</el-button>
                </div>
            </div>
        </template>
        <!-- 搜索表单 -->

        <!-- 文章列表 -->
        <el-table :data="dataList" border  style="width: 100%" @selection-change="handleSelectionChange">


          <el-table-column label="资源类型" >
            <template v-slot="scope">
              <span v-if="scope.row.type==1">星星</span>
              <span v-if="scope.row.type==2">矿石</span>
              <span v-if="scope.row.type==3">头像</span>
              <span v-if="scope.row.type==4">头像框</span>
              <span v-if="scope.row.type==5">气泡</span>
              <span v-if="scope.row.type==6">表情</span>
              <span v-if="scope.row.type==7">表情包</span>
              <span v-if="scope.row.type==8">谢谢惠顾</span>
              <span v-else ></span>
            </template>
          </el-table-column>
            <el-table-column label="排序（从小到大）" prop="sort"></el-table-column>
          <el-table-column label="背景图片">
            <template v-slot="scope">
              <img v-if="scope.row.backgroundUrl" :src="scope.row.backgroundUrl" width="40" height="40" class="head_pic" />
            </template>
          </el-table-column>

            <el-table-column label="创建时间" width="200px" prop="createTime"></el-table-column>

            <el-table-column label="操作"  width="150px">
                <template #default="{ row }">
                    <el-button :icon="Edit" circle plain type="primary" @click="showData(row)"></el-button>
                  <el-button :icon="Delete" circle plain type="danger" @click="delData(row)"></el-button>
                </template>
            </el-table-column>
            <template #empty>
                <el-empty description="没有数据" />
            </template>
        </el-table>




        <!-- 抽屉 -->
        <el-drawer v-model="visibleDrawer" :title="title" direction="rtl" size="50%">
            <!-- 添加文章表单 -->
            <el-form :model="dataModel" label-width="100px">



              <el-form-item label="奖励类型">
                <el-select placeholder="请选择" v-model="dataModel.type">
                  <el-option v-for="c in resourceTypes" :key="c.id" :label="c.resourceValue" :value="c.id">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="排序">
                <el-input-number v-model="dataModel.sort" />
              </el-form-item>
              <el-form-item label="背景图片" >
                <el-upload class="avatar-uploader" :auto-upload="true" :show-file-list="false"
                           action="/api/admin/upload/uploadFile/v2" name="file"
                           :headers="{ 'Authorization': tokenStore.token }"
                           :on-success="uploadBackgroundUrlSuccess">
                  <img v-if="dataModel.backgroundUrl" :src="dataModel.backgroundUrl" class="avatar"/>
                  <el-icon v-else class="avatar-uploader-icon">
                    <Plus/>
                  </el-icon>
                </el-upload>
                <span v-if="show">{{ dataModel.backgroundUrl }}</span>
                <el-icon v-if="dataModel.backgroundUrl" @click="delBackgroundUrl()">
                  <Close/>
                </el-icon>
              </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="title == '添加抽奖' ? addData() : updateData()">保存</el-button>

                </el-form-item>
            </el-form>
        </el-drawer>


    </el-card>
</template>
<style lang="scss" scoped>
.page-container {
    min-height: 100%;
    box-sizing: border-box;

    .header {
        display: flex;
        align-items: center;
        justify-content: space-between;
    }
}


/* 抽屉样式 */
.avatar-uploader {
    :deep() {
        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }

        .el-upload {
            border: 1px dashed var(--el-border-color);
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
            transition: var(--el-transition-duration-fast);
        }

        .el-upload:hover {
            border-color: var(--el-color-primary);
        }

        .el-icon.avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            text-align: center;
        }
    }
}

.editor {
    width: 100%;

    :deep(.ql-editor) {
        min-height: 200px;
    }
}
</style>